package cannot.backcontroller;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cannot.pojo.Tv;
import cannot.pojo.User;
import cannot.service.HistoryService;
import cannot.service.PraiseService;
import cannot.service.RemarkService;
import cannot.service.TvService;
import cannot.service.TypeTvService;
import cannot.service.UserService;
import cannot.util.StoragePlace;

@Controller
public class TvManageController {
	@Autowired
	TvService tvService;
	@Autowired
	UserService userService;
	@Autowired
	TypeTvService typeTvService;
	@Autowired
	RemarkService remarkService;
	@Autowired
	HistoryService historyService;
	@Autowired
	PraiseService praiseService;
	
	@RequestMapping("admin_rightContent_manageTv")
	public String manageTvPage(Model model){
		List<Tv> tvs = tvService.listTvs();
		for(Tv tv:tvs){
			User u = userService.get((long)tv.getUploader_id());
			tv.setUser(u);
		}
		model.addAttribute("tvs", tvs);
		return "admin/rightContent/manageTv";
	}
	
	@RequestMapping("delTv")
	@ResponseBody
	public String delTv(Integer id){
		Tv tv = tvService.selectById(id);
		//删除视频
		String videoPath = StoragePlace.basicPlace+"tv"+File.separator;
		String videoUrl = tv.getVideo_url();
		File f = new File(videoPath+videoUrl);
		f.delete();
		//删除图片
		String imgPath =StoragePlace.basicPlace+"img"+File.separator;
		String imgUrl =tv.getImg_url();
		File f1 = new File(imgPath+imgUrl);
		f1.delete();
		//删除type_tv对应
		typeTvService.deleteByTvId(id);
		
		//删除评论
		remarkService.deleteByTvId(id);
		//删除点赞
		praiseService.deleteByTvId(id);
		//删除观看历史
		historyService.deleteByTvId(id);
		//删除视频
		tvService.deleteTvById(id);
		return null;
	}
}
